home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ccs_util.jar / com / commerceone / util / identity / LongId.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-12-09  |  2.5 KB  |  72 lines

  1. package com.commerceone.util.identity;
  2.  
  3. import java.net.InetAddress;
  4.  
  5. public class LongId implements Identity {
  6.    private static final byte[] s_hostaddr = getHostAddress();
  7.    private static long s_count = 0L;
  8.    private long m_id = 0L;
  9.  
  10.    public String toString() {
  11.       return "" + this.m_id;
  12.    }
  13.  
  14.    public int hashCode() {
  15.       Long obj_id = new Long(this.m_id);
  16.       return obj_id.intValue();
  17.    }
  18.  
  19.    public static LongId create() {
  20.       long id = s_count++ & 65535L;
  21.       id |= (long)(s_hostaddr[3] << 16);
  22.       id |= (long)(s_hostaddr[2] << 24);
  23.       return new LongId(id);
  24.    }
  25.  
  26.    private LongId(long id) {
  27.       this.m_id = id;
  28.    }
  29.  
  30.    private static byte[] getHostAddress() {
  31.       try {
  32.          return InetAddress.getLocalHost().getAddress();
  33.       } catch (Exception var1) {
  34.          return new byte[4];
  35.       }
  36.    }
  37.  
  38.    public static LongId internalize(long id) {
  39.       return new LongId(id);
  40.    }
  41.  
  42.    public static void main(String[] args) {
  43.       for(int i = 0; i < 100; ++i) {
  44.          byte[] bytes = create().toBytes();
  45.          System.out.println(bytes[0] + " " + bytes[1] + " " + bytes[2] + " " + bytes[3]);
  46.       }
  47.  
  48.    }
  49.  
  50.    public boolean equals(Object obj) {
  51.       if (!(obj instanceof LongId)) {
  52.          return false;
  53.       } else {
  54.          return this.m_id == ((LongId)obj).m_id;
  55.       }
  56.    }
  57.  
  58.    public byte[] toBytes() {
  59.       long value = this.m_id;
  60.       byte[] bytes = new byte[4];
  61.       bytes[0] = (byte)((int)(value >> 24 & 255L));
  62.       bytes[1] = (byte)((int)(value >> 16 & 255L));
  63.       bytes[2] = (byte)((int)(value >> 8 & 255L));
  64.       bytes[3] = (byte)((int)(value & 255L));
  65.       return bytes;
  66.    }
  67.  
  68.    public int formatHint() {
  69.       return 1;
  70.    }
  71. }
  72.